##  pip3 install baidu-aip
import base64
from aip import AipFace, AipSpeech
from pypinyin import lazy_pinyin, Style

#
# """ 你的 APPID AK SK """
# APP_ID = '119414949'
# API_KEY = 'gMVpzD4hiRMwSeojYrRlRxs6'
# SECRET_KEY = 'dSM392eHbW9Lck8enb7yIU8XV62nfyHc'
#
# client = AipFace(APP_ID, API_KEY, SECRET_KEY)

class BaiduFace:
    def __init__(self, APP_ID='119414949', APP_KEY='gMVpzD4hiRMwSeojYrRlRxs6',
                 APP_SECRET='dSM392eHbW9Lck8enb7yIU8XV62nfyHc'):
        self.APP_ID = APP_ID
        self.APP_KEY = APP_KEY
        self.APP_SECRET = APP_SECRET

        self.client = AipFace(self.APP_ID, self.APP_KEY, self.APP_SECRET)

    # 注册人脸  611223cdffb97a8ce416e0ef9dbd5bc1
    def add_user(self, file_obj, userId):
        image = base64.b64encode(file_obj.read()).decode('utf-8')
        imagType = "BASE64"
        groupId = "mingle"
        userId = userId   # "fengjuan" 用人名的拼音

        """ 调用人脸注册 """
        # client.addUser(image, imagType, groupId, userId);
        options = {}
        options["user_info"] = "wife"
        options["quality_control"] = "NORMAL"
        options["liveness_control"] = "LOW"
        options["action_type"] = "REPLACE"

        """ 带参数调用人脸注册 """
        res = self.client.addUser(image, imagType, groupId, userId, options)
        print(res)
        return res

    # 删除人脸
    def delete(self, userId, faceToken):
        userId = userId
        groupId = "mingle"
        faceToken = faceToken  # 传入录入时生成的 face_token =  d30aae77fcf418fdaee4f8f3a93b3d3c

        """ 调用人脸删除 """
        res = self.client.faceDelete(userId, groupId, faceToken)
        return res

    # 搜索人脸
    def search(self, file_obj):
        data = base64.b64encode(file_obj.read()).decode('utf-8')
        image = data
        imageType = "BASE64"
        groupIdList = "mingle,test"

        """ 调用人脸搜索 """
        self.client.search(image, imageType, groupIdList)

        """ 如果有可选参数 """
        options = {}
        options["match_threshold"] = 70
        options["quality_control"] = "NORMAL"
        options["liveness_control"] = "LOW"
        # options["user_id"] = "233451"

        options["max_user_num"] = 3
        # 带参数调用人脸搜索
        res = self.client.search(image, imageType, groupIdList, options)
        return res

    # 姓名转拼音
    def name_pinyin(self, name):
        style = Style.TONE3
        name_list = lazy_pinyin(name, style=style)  # zhu1xiao3ming2/wen2feng4juan1 数字为声调
        return ''.join(name_list)


class BaiduVoice:
    def __init__(self, APP_ID='119434277', API_KEY='n2nuIBggw2pUY6Oq6m9gCmmJ', SECRET_KEY='mV1IRCj07fXGXk18LHbJNLTQqcbL9Unx'):
        """ 你的 APPID AK SK """
        self.APP_ID = APP_ID
        self.API_KEY = API_KEY
        self.SECRET_KEY = SECRET_KEY
        self.client = AipSpeech(self.APP_ID, self.API_KEY, self.SECRET_KEY)

    def speed(self, voice_object):
        res = self.client.asr(voice_object.read(), 'pcm', 16000, {
            'dev_pid': 1537,
        })
        return res

#
# if __name__ == '__main__':
#     ai = BaiduVoice()
#     obj = open('./test.wav', 'rb')
#     res = ai.speed(obj) # {'corpus_no': '7523977719586954070', 'err_msg': 'success.', 'err_no': 0, 'result': ['欢迎你来到我们的社区，请随意参观一下。'], 'sn': '723655635111751812575'}
#     print(res)